const app = getApp()
Page({
  data: {
    goodList: [
      {
        shoName: '小明零食',
        status: 0,
        title: '【超大颗】樱桃【超大颗】樱桃【超大颗】樱桃【超大颗】樱桃【超大颗【超大颗】樱桃【超大颗】樱',
        goodImg: '../../../assets/images/banner03.jpg',
        goodLogo: '../../../assets/images/banner01.jpg',
        price: '100',
        num: 1
      }
    ],
    address: {
      userName: '',
      telNumber: '',
      detailInfo: '',
      address_id: ''
    },
    order_sn: '',
    create_time: '',
    order_price: '',
    goodLength: 0,
    shop_name: '',
    shopLogo: '',
    order_status: 2,
    enterType: 'init'
  },
  onLoad: function (options) {
    this.setData({
      order_sn: options.order_sn,
      enterType: options.enterType
    })
    console.log(options)
    this.getOrderDetail({ order_sn: this.data.order_sn })
  },
  onShow: function () {
    this.setData({
      enterType: app.Store.get("enterType")
    })
  },
  onUnload: function () {
    app.Store.set("enterType", this.data.enterType)
  },

  onPullDownRefresh: function () {

  },
  getOrderDetail(obj) {
    app.Http.http({
      url: app.Api.OrderDetail,
      method: 'POST',
      params: obj
    }).then(data => {
      if (data.goodsList.length > 0) {
        this.setData({
          goodList: data.goodsList
        })
      } else {
        this.setData({
          goodList: []
        })
      }
      this.setData({
        address: {
          userName: data.userName,
          telNumber: data.telNumber,
          detailInfo: data.detailInfo,
          address_id: data.address_id
        },
        order_status: data.order_status,
        shop_name: data.shop_name,
        shopLogo: data.shop_logo,
        goodLength: data.goodsList.length,
        order_price: data.order_price,
        create_time: app.formatTime(data.create_time)
      })

    }).catch(err => {
      console.log(err)
    })
  },
  pay() {
    app.payOrder({ order_sn: this.data.order_sn }, (data) => {
      console.log(data)
    })
  },
  cancel() {
    let _this = this;
    let url = this.data.enterType === 'user' ? app.Api.OrderShopCancel : app.Api.OrderCancel
    wx.showModal({
      title: '温馨提示',
      content: '确定要取消该订单吗？',
      success(res) {
        if (res.confirm) {
          app.Http.http({
            url: url,
            method: 'POST',
            params: {
              order_sn: _this.data.order_sn
            }
          }).then(data => {
            _this.getOrderDetail({ order_sn: _this.data.order_sn })
          }).catch(err => {
            console.log(err)
          })
        }
      }
    })
  }
})